function formatTime(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}

//验证身份证号是否符合格式
function isValidIDCardNo(idCardStr) {
    var result = idCardStr.match(/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/);
    if (result == null)
        return false;

    if (idCardStr.length == 18) {
        var sBirthday = idCardStr.substr(6, 4) + "-" + Number(idCardStr.substr(10, 2)) + "-" + Number(idCardStr.substr(12, 2));
        var d = new Date(sBirthday.replace(/-/g, "/"));
        if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate()))
            return false;

        var iSum = 0;
        var sId = idCardStr.replace(/x$/i, "a");
        for (var i = 17; i >= 0; i--)
            iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);

        if (iSum % 11 != 1)
            return false;
    } else {
        var sBirthday = "19" + idCardStr.substr(6, 2) + "-" + Number(idCardStr.substr(8, 2)) + "-" + Number(idCardStr.substr(10, 2));
        var d = new Date(sBirthday.replace(/-/g, "/"));
        if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate()))
            return false;
    }

    return true;
}
//验证手机号码是否符合格式
function checkPhone(phone) {
    if (!/^1[34578]\d{9}$/.test(phone) || phone === '') {
        return false;
    }
    return true;
}
//验证银行卡
function checkBankNo(bank) {
    if (!(/^(\d{16}|\d{19})$/.test(bank)) || bank === '') {
        return false;
    }
    return true;
}




//银行卡号的控制及格式化
function formatBankNo(BankNo) {
    if (BankNo.value == "") return;
    var account = new String(BankNo.value);
    account = account.substring(0, 22); /*帐号的总数, 包括空格在内 */
    if (account.match(".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null) {
        /* 对照格式 */
        if (account.match(".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" + ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" +
                ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" + ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null) {
            var accountNumeric = accountChar = "",
                i;
            for (i = 0; i < account.length; i++) {
                accountChar = account.substr(i, 1);
                if (!isNaN(accountChar) && (accountChar != " ")) accountNumeric = accountNumeric + accountChar;
            }
            account = "";
            for (i = 0; i < accountNumeric.length; i++) { /* 可将以下空格改为-,效果也不错 */
                if (i == 4) account = account + " "; /* 帐号第四位数后加空格 */
                if (i == 8) account = account + " "; /* 帐号第八位数后加空格 */
                if (i == 12) account = account + " "; /* 帐号第十二位后数后加空格 */
                account = account + accountNumeric.substr(i, 1)
            }
        }
    } else {
        account = " " + account.substring(1, 5) + " " + account.substring(6, 10) + " " + account.substring(14, 18) + "-" + account.substring(18, 25);
    }
    if (account != BankNo.value) BankNo.value = account;
}

//判断数组中是否含有某个值
function contains(arr, obj) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === obj) {
            return true;
        }
    }
    return false;
}

function myIndexOf(arr, el) {
    for (var i = 0, n = arr.length; i < n; i++) {
        if (arr[i] === el) {
            return i;
        }
    }
    return -1;
}

//验证是否是数字或者金额
function checkMoneyNum(number) {
    if (isNaN(number) && (!(/^(([1-9][0-9]{0,2}(,\d{3})*)|0)(\.\d{1,2})?$/.test(number)))) {
        return false;
    } else {
        return true;
    }
}

//对象的深度克隆
function deepClone(obj) {
    var result, oClass = isClass(obj);
    //确定result的类型
    if (oClass === "Object") {
        result = {};
    } else if (oClass === "Array") {
        result = [];
    } else {
        return obj;
    }
    for (var key in obj) {
        var copy = obj[key];
        if (isClass(copy) == "Object") {
            result[key] = arguments.callee(copy); //递归调用
        } else if (isClass(copy) == "Array") {
            result[key] = arguments.callee(copy);
        } else {
            result[key] = obj[key];
        }
    }
    return result;
}

//返回传递给他的任意对象的类
function isClass(o) {
    if (o === null) return "Null";
    if (o === undefined) return "Undefined";
    return Object.prototype.toString.call(o).slice(8, -1);
}


/**
 * 将数值四舍五入(保留2位小数)后格式化成金额形式
 *
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.45'
 * @type String
 */
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    var sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    var cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}

//去掉字符串中所有空格的方法
function Trim(str,is_global)
    {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g,"");
        if(is_global.toLowerCase()=="g")
        {
            result = result.replace(/\s/g,"");
        }
        return result;
}

/*
关于网址https://www.riskall.cn/fastapply/ManagerAction.do?method=createContractInfo的截取域名https://www.riskall.cn/；
*/ 
const domain='https://www.riskall.cn/';//测试版域名
// const domain='https://www.riskall.net/';//生产版域名

module.exports = {
    formatTime: formatTime,
    formatNumber: formatNumber,
    isValidIDCardNo: isValidIDCardNo,
    checkPhone: checkPhone,
    contains: contains,
    myIndexOf: myIndexOf,
    deepClone: deepClone,
    formatCurrency: formatCurrency,
    checkBankNo: checkBankNo,
    Trim: Trim,
    domain:domain,
}